package cn.dengta.webapp.user.entity;

import java.util.Date;
import java.util.Map;

import cn.dengta.common.util.Formats;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.CollectionUtil;
import me.codeplayer.util.X;

/**
 * 用户设备日志
 *
 * @author lgy
 * @since 2022-08-16
 */
@Setter
@Getter
@Accessors(chain = true)
public class DeviceLog extends TimeBasedEntity {

	/** 用户Id */
	private Long userId;
	/** 设备 */
	private String device;
	/** 注册次数 */
	private Integer registerTimes;
	/** 冻结时间 */
	private Date freezeTime;
	/** 状态 */
	private Integer status;
	/** 备注 */
	private String remark;
	/** 操作人Id */
	private Long empId;

	public DeviceLog init(DeviceLog input, Date now) {
		this.userId = X.expectNotNull(userId, input.userId);
		this.device = X.expectNotNull(device, input.device);
		this.freezeTime = X.expectNotNull(input.freezeTime, freezeTime, now);
		this.registerTimes = X.expectNotNull(input.registerTimes, registerTimes, 0);
		this.status = X.expectNotNull(input.status, status, 1);
		super.initTime(now);
		return this;
	}

	public boolean isFrozen() {
		return freezeTime != null && freezeTime.getTime() > System.currentTimeMillis();
	}

	public Integer addTimes() {
		return ++registerTimes;
	}

	public static final String USER_ID = "user_id";
	public static final String DEVICE = "device";
	public static final String FREEZE_TIME = "freeze_time";
	public static final String REGISTER_TIMES = "register_times";
	public static final String STATUS = "status";

	public Map<String, Object> asEmbed() {
		return asEmbed(this, t -> CollectionUtil.ofHashMap("id", t.getId(),
				"remark", t.getRemark(), "operationTime_", Formats.formatDefDate(t.getUpdateTime()),
				"freezeTime_", Formats.formatDefDate(t.getFreezeTime())));
	}

}
